﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public class SettingXMLHelper
{
    private static readonly string XMLPATH = HttpContext.Current.Server.MapPath("~/Config/setting.xml");
    public static List<Setting> GetSettings()
    {
        var doc = new XmlDocument();
        doc.Load(XMLPATH);
        var nodes = doc.SelectNodes("/settings/db");
        var settings = new List<Setting>();
        foreach (XmlNode node in nodes)
        {
            if (node.Attributes["name"] == null || node.Attributes["connectionString"] == null || node.Attributes["dbType"] == null)
                continue;
            settings.Add(new Setting()
            {
                name = node.Attributes["name"].InnerText,
                connectionString = node.Attributes["connectionString"].InnerText,
                dbType = node.Attributes["dbType"].InnerText 
            });
        }
        return settings;
    }


    public static Setting AddSetting(string name, string dbType, string connectionString)
    {
       return AddSetting(new Setting()
        {
            name = name,
            dbType = dbType,
            connectionString = connectionString
        });
    }

    public static Setting AddSetting(Setting setting)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(XMLPATH);
        var root = doc.SelectSingleNode("settings");
        var ele = doc.CreateElement("db");
        ele.SetAttribute("name", setting.name);
        ele.SetAttribute("dbType", setting.dbType);
        ele.SetAttribute("connectionString", setting.connectionString);
        ele.SetAttribute("addTime", DateTime.Now.ToString());

        //if (string.IsNullOrEmpty(setting.id))
        //    setting.id = Guid.NewGuid().ToString();
        //ele.SetAttribute("id", setting.id);

        root.AppendChild(ele);
        doc.Save(XMLPATH);
        return setting;
    }

    public static bool ExistSetting(string connectionString)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(XMLPATH);
        var nodes = doc.SelectNodes("/settings/db");
        foreach (XmlNode node in nodes)
        {
            if (node.Attributes["connectionString"] != null && string.Compare(node.Attributes["connectionString"].InnerText, connectionString, true) == 0)
                return true;
        }
        return false;
    }


    public static void RemoveSetting(string connectionString)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(XMLPATH);
        var nodes = doc.SelectNodes("/settings/db");
        foreach (XmlNode node in nodes)
        {
            if (node.Attributes["connectionString"] != null && node.Attributes["connectionString"].InnerText == connectionString)
                node.ParentNode.RemoveChild(node);
        }
        doc.Save(XMLPATH);
    }
}